import os, io
import pyheif
from PIL import Image


def heic2img(heicPath, outFormat='png'):
    with open(heicPath, 'rb') as f:
        bytesIo = f.read()
        i = pyheif.read_heif(bytesIo)
        # print('i = ', i)
        # print('i.metadata = ', i.metadata)
        pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
        # print('pi = ', pi)
        name, ext=os.path.splitext(heicPath)
        outPath=name+'.'+outFormat
        c=0
        while os.path.exists(outPath):
            outPath=name+'_'+str(c)+'.'+outFormat
            c+=1
        pi.save(outPath, format=outFormat)
        

 
if __name__ == "__main__":
    heic2img('/run/media/u/dcf782ac-e43c-4b4d-83c7-662c8900c95f/soft/2021/AA/AA Baggage 2D sample/Belt_1.HEIC', 'jpeg')

